package com.itclass.auth.handler;

import cn.hutool.json.JSONUtil;
import com.itclass.auth.pojo.db.Result;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class RedisLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 设置响应内容类型为 JSON
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        // 设置响应字符编码为 UTF-8
        response.setCharacterEncoding("UTF-8");
        // 创建一个包含错误信息的 JSON 对象
        String json = JSONUtil.toJsonStr(Result.ok().message("退出成功"));
        response.getOutputStream().write(json.getBytes(StandardCharsets.UTF_8));
    }
}
